#include "stdafx.h"
#include "win_splitter.h"


win_splitter::win_splitter() : _iSplitterMargin(0), _isDragging(false)
{
    _dwExStyle = WS_EX_TOOLWINDOW;
    _dwStyle = WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
}


win_splitter::~win_splitter()
{
}

int win_splitter::fit(const RECT &rcFragment)
{
    memcpy(&_rcWnd, &rcFragment, sizeof(_rcWnd));

    if(_iAlign & TILE_TOP)
    {
        if(rcFragment.bottom - rcFragment.top <= _iTileHeight)
        {
            return 1;
        }
        _rcWnd.bottom = _rcWnd.top + _iSplitterMargin + _iTileHeight;
        if(_rcWnd.bottom > rcFragment.bottom)
        {
            _iSplitterMargin -= _rcWnd.bottom - rcFragment.bottom;
            _rcWnd.bottom = rcFragment.bottom;
        }
        _rcWnd.top = _rcWnd.bottom - _iTileHeight;
        return 1;
    }

    if(_iAlign & TILE_BOTTOM)
    {
        if(rcFragment.bottom - rcFragment.top <= _iTileHeight)
        {
            return 1;
        }
        _rcWnd.top = _rcWnd.bottom - _iSplitterMargin - _iTileHeight;
        if(_rcWnd.top < rcFragment.top)
        {
            _iSplitterMargin += _rcWnd.top - rcFragment.top;
            _rcWnd.top = rcFragment.top;
        }
        _rcWnd.bottom = _rcWnd.top + _iTileHeight;
        return 1;
    }


    if(_iAlign & TILE_LEFT)
    {
        if(rcFragment.right - rcFragment.left <= _iTileHeight)
        {
            return 1;
        }
        _rcWnd.right = _rcWnd.left + _iSplitterMargin + _iTileWidth;
        if(_rcWnd.right > rcFragment.right)
        {
            _iSplitterMargin -= _rcWnd.right - rcFragment.right;
            _rcWnd.right = rcFragment.right;
        }
        _rcWnd.left = _rcWnd.right - _iTileWidth;
        return 1;
    }

    if(_iAlign & TILE_RIGHT)
    {
        if(rcFragment.right - rcFragment.left <= _iTileHeight)
        {
            return 1;
        }
        _rcWnd.left = _rcWnd.right - _iSplitterMargin - _iTileWidth;
        if(_rcWnd.left < rcFragment.left)
        {
            _iSplitterMargin += _rcWnd.left - rcFragment.left;
            _rcWnd.left = rcFragment.left;
        }
        _rcWnd.right = _rcWnd.left + _iTileWidth;
        return 1;
    }


    return 1;
}


int win_splitter::fit_last(const RECT &rcFragment)
{
    return fit(rcFragment);
}



LRESULT win_splitter::OnPaint()
{
    PAINTSTRUCT ps;
    RECT rc;

    HDC hdc = BeginPaint(_hWnd, &ps);
    GetClientRect(_hWnd, &rc);
    Rectangle(hdc, rc.left, rc.top, rc.right, rc.bottom);
    EndPaint(_hWnd, &ps);

    return 0;
}

LRESULT win_splitter::OnLeftDown(WORD x, WORD y, BOOL bCtrlDown, BOOL bShiftDown)
{
    SetCapture(_hWnd);
    GetCursorPos(&_ptDrag);
    _isDragging = true;
    _iMarginOnDraged = _iSplitterMargin;
    return 0;
}

LRESULT win_splitter::OnLeftUp(WORD x, WORD y, BOOL bCtrlDown, BOOL bShiftDown)
{
    _isDragging = false;
    ReleaseCapture();
    return 0;
}

LRESULT win_splitter::OnMouseMove(DWORD dwKeys, WORD x, WORD y)
{
    if(_isDragging)
    {
        POINT ptCur;
        GetCursorPos(&ptCur);

        if(_iAlign & TILE_TOP)
        {
            _iSplitterMargin = _iMarginOnDraged - _ptDrag.y + ptCur.y;
        }
        else if(_iAlign & TILE_BOTTOM)
        {
            _iSplitterMargin = _iMarginOnDraged + _ptDrag.y - ptCur.y;
        }
        else if(_iAlign & TILE_LEFT)
        {
            _iSplitterMargin = _iMarginOnDraged - _ptDrag.x + ptCur.x;
        }
        else if(_iAlign & TILE_RIGHT)
        {
            _iSplitterMargin = _iMarginOnDraged + _ptDrag.x - ptCur.x;
        }

        if(_iSplitterMargin < 0 )
        {
            _iSplitterMargin = 0;
        }

        if(_pWndFrame)
        {
            _pWndFrame->auto_layout();
        }
    }
    return 0;
}
